<?php

/**
 * created by zzy
 * date: 2018/1/26 14:07
 */

namespace App\Api\Service\Buyers\Controllers;

use App\Eloquent\Ygt\Buyers;
use App\Eloquent\Ygt\DepartmentUser;
use App\Engine\Func;
use Framework\BaseClass\Api\Controller;

class BuyersController extends Controller
{
    public $userId          = 25;
    public function index()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $buyersId           = request('buyers_id',0);
        if( !$userInfo || !$buyersId)
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $where              = ['id'=>$buyersId,'company_id'=>$companyId];
        $columns            = 'id,buyer_name,license,grant_img,trademark,other_img';
        $info               = Buyers::getInfo($where,$columns);
        $buyers             = [];
        if($info)
        {
            $buyers         = $info->toArray();
            $license        = $buyers['license'];
            $grantImg       = $buyers['grant_img'];
            $trademark      = $buyers['trademark'];
            $otherImg       = $buyers['other_img'];
            $otherImgArr    = [];
            if($otherImg)
            {
                $otherImgArr    = explode(',',$otherImg);
            }
            $licensePath    = Func::getImgUrlById($license,false);
            $grantImgPath   = Func::getImgUrlById($grantImg,false);
            $trademarkPath  = Func::getImgUrlById($trademark,false);
            $otherImgPathArr= Func::getImgUrlByIds($otherImg,false);
            $buyers['otherImgArr']      = $otherImgArr;
            $buyers['licensePath']      = $licensePath;
            $buyers['grantImgPath']     = $grantImgPath;
            $buyers['trademarkPath']    = $trademarkPath;
            $buyers['otherImgPathArr']  = $otherImgPathArr;
        }
        return $buyers;
    }
    public function modify()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $buyersId           = request('buyers_id',0);
        if( !$userInfo || !$buyersId)
        {
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $params             = request()->all();
        $data               = [];
        if(isset($params['license']))
        {
            $data['license']    = intval($params['license']);
        }
        if(isset($params['grant_img']))
        {
            $data['grant_img']  = intval($params['grant_img']);
        }
        if(isset($params['trademark']))
        {
            $data['trademark']  = intval($params['trademark']);
        }
        if(isset($params['other_img']))
        {
            //$otherImgStr            = implode(',',$params['other_img']);
            $data['other_img']    = $params['other_img'];
        }
        if(empty($data))
        {
            xThrow(ERR_PARAMETER, '未修改任何内容');
        }
        $where              = ['id'=>$buyersId,'company_id'=>$companyId];
        $updateResult       = Buyers::updateOneData($where,$data);
        if($updateResult === false)
        {
            xThrow(ERR_UNKNOWN);
        }
        return ['message'=>'编辑成功'];
    }
}